<?php

namespace App\Admin\Actions\Aliyun;

use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use App\Facades\AliyunDcdn;
use App\Models\Aliyun;
use Dcat\Admin\Actions\Response;
use Dcat\Admin\Grid\BatchAction;
use Illuminate\Http\Request;

class BatchPerformConfig extends BatchAction
{
    protected string $title = '批量载入优化配置';

    public function __construct(public Aliyun $aliyun)
    {
        parent::__construct();
    }

    /**
     * @param  Request  $request
     * @return Response
     * @throws ClientException
     * @throws ServerException
     */
    public function handle(Request $request): Response
    {
        $model = $request->get('aliyun');
        $aliyun = Aliyun::ofAdmin()->find($model);

        AliyunDcdn::aliyun($aliyun)->perform($this->getKey());

        return $this->response()->success('操作成功')->refresh();
    }

    public function confirm(): array
    {
        return [
            '载入优化配置',
            '开启【页面优化】，【Gzip压缩】，【Brotli压缩】？',
        ];
    }

    public function parameters(): array
    {
        return [
            'aliyun' => $this->aliyun->id,
        ];
    }
}
